package ServerThreads;

import Communication.TaskForModule;
import Communication.TaskForServer;
import Const.ModuleConst;
import Const.ServerConst;
import LiftEngine.Lift;
import Module.ModuleClient;
import Server.ClientList;
import Server.ModuleList;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;

/**
 *
 * Class description goes here.
 *
 * @author Kamil Gajowy
 */
public class SListenerModule extends MyThreadServer {

     private ServerSocket servSock;
     private int nextID = 0;
     private LinkedList<Thread> moduleCommunication;
     private LinkedList<TaskForServer> taskList;
     private LinkedList<TaskForModule> taskMList;

    /**
    * Default constructor.
     * @param name
     * @param cListReference
     * @param mRerefence
     * @param liftRef
     * @param tList
     * @param taskMList 
     */
    public SListenerModule(String name,
            ClientList cListReference,
            ModuleList mRerefence,
            Lift liftRef,
            LinkedList<TaskForServer> tList,
            LinkedList<TaskForModule> taskMList){
        super(name, cListReference, mRerefence, liftRef);
        this.moduleCommunication = new LinkedList<Thread>();
        this.taskList = tList;
        this.taskMList = taskMList;
    }

    @Override
    public void run() {
        init();
        while(serverRunning){
            //accept listeners
            Socket clntSock;
            try {
                clntSock = servSock.accept(); // Get client connection
                String address = clntSock.getInetAddress().getHostAddress();
                int port = clntSock.getPort();
                System.out.println("Incoming Module-Client at "
                        + address + " on port " + port);
                //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TUTAJ DOPISALEM getLiftReference(),0 zeby konstrukotr Modulu klienta sie zgadzal ;X
                ModuleClient newModule = new ModuleClient(clntSock,nextID,ModuleConst.TypeUnknown, getLiftReference(),0);
                Thread fork = new SModuleCommunication(
                        "Module"+newModule.getId(),
                        this.getClientList(),
                        this.getLiftReference(),
                        newModule,
                        taskList,
                        taskMList);
                moduleCommunication.add(fork);
                fork.start();
                
                this.getModuleReference().addModule(newModule);
                nextID++;

                System.out.println("\tConnection estabilished.");

            } catch (Exception ex) {
                System.out.println("Module rejected. Reason:"
                        + ex.getMessage() + "\n" + ex.getClass());
            }
        }
    }//end run

    private boolean init() {
        System.out.println("\t\t\t opening socket for modules...");
        try {
            servSock = new ServerSocket(ServerConst.PortModule);
            return true;
        } catch (IOException ex) {
            return false;
        }
    }




}//end class
